package com.fs.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.fs.common.core.page.PageDomain;
import com.fs.common.util.page.PageUtils;
import com.fs.sale.mapper.PriceDetailedMapper;
import com.fs.sale.mapper.PriceListMapper;
import com.fs.sale.pojo.PriceDetailed;
import com.fs.sale.pojo.PriceList;
import com.fs.sale.service.PriceListService;
import com.fs.sale.vo.OrderSearch;
import com.fs.sale.vo.PriceListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.system.mapper.SysUserMapper;
import com.fs.system.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PriceListServiceImpl implements PriceListService {
   @Autowired
    PriceListMapper priceListMapper;
   @Autowired
    PriceDetailedMapper priceDetailedMapper;
   @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<PriceListVo> selectAll(Page<PriceListVo> page,OrderSearch orderSearch) {
        return priceListMapper.selectAll(page,orderSearch);
    }

    @Override
    public List<PriceListVo> selectById(Long id,OrderSearch orderSearch,Page<PriceListVo> page) {
        return priceListMapper.queryByID(id,orderSearch,page);
    }

    @Override
    public Integer toExaminePriceList(Integer id,Integer newStatus) {
        if(sysUserMapper.selectPostNameByUserId(SecurityUtils.getUserId()).equals("普通员工")){
            return null;
        }
        QueryWrapper<PriceList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pl_id",id);
        PriceList priceList = priceListMapper.selectOne(queryWrapper);
        priceList.setStatus(newStatus);
        UpdateWrapper<PriceList> updateWrapper=new UpdateWrapper();
        updateWrapper.eq("pl_id",id);
        return priceListMapper.update(priceList,updateWrapper);
    }

    @Override
    public void addPriceList(PriceList priceList) {
        priceList.setUserId(SecurityUtils.getUserId().intValue());
      priceListMapper.insert(priceList);
        System.out.println(priceList.getPlId());
      int id=priceList.getPlId();
      for(PriceDetailed priceDetailed:priceList.getPriceDetailedList()){
          priceDetailed.setPlId(id);
          priceDetailedMapper.insert(priceDetailed);
        }
    }

    @Override
    public Page<PriceListVo> select(PageDomain pageDomain,OrderSearch orderSearch) {
        Page<PriceListVo> page=new Page<>(pageDomain.getPageNum(),pageDomain.getPageSize());
        if(pageDomain.getIsAsc().equalsIgnoreCase("asc")){ //升序
            page.addOrder(OrderItem.asc(pageDomain.getOrderByColumn()));
        }else if(pageDomain.getIsAsc().equalsIgnoreCase("desc")){ //降序
            page.addOrder(OrderItem.desc(pageDomain.getOrderByColumn()));
        }
        String post=sysUserMapper.selectPostNameByUserId(SecurityUtils.getUserId());
        if(post.equals("普通员工")){
            Long id=SecurityUtils.getUserId();
            page.setRecords(modifyStatus(selectById(id,orderSearch,page)));
            return page;
        }
         page.setRecords(modifyStatus(selectAll(page,orderSearch)));
        return page;
    }

    public List<PriceListVo> modifyStatus(List<PriceListVo> list){
        for(PriceListVo priceListVo:list){
            switch (priceListVo.getStatus()){
                case 0:
                    priceListVo.setStatusStr("待审核");
                    break;
                case 1:
                    priceListVo.setStatusStr("审核通过");
                    break;
                case 2:
                    priceListVo.setStatusStr("审核未通过");
                    break;


            }
        }
        return list;
    }
}
